package com.atguigu.gmall.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.item.service.ItemService;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.client.ProductFeignClient;
import io.reactivex.rxjava3.core.Completable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private ListFeignClient listFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Override
    public Map<String, Object> getSkuInfo(Long skuId) {
        HashMap<String, Object> map = new HashMap<>();

        try {
            CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
                SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
                map.put("skuInfo", skuInfo);
                return skuInfo;
            },threadPoolExecutor);

            CompletableFuture<Void> priceCompletableFuture = CompletableFuture.runAsync(() -> {
                BigDecimal price = productFeignClient.getSkuPrice(skuId);
                map.put("price", price);
            },threadPoolExecutor);

            CompletableFuture<Void> categoryViewCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
                BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
                map.put("categoryView", categoryView);
            },threadPoolExecutor);

            CompletableFuture<Void> valueCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {

                Map idsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
                String strJson = JSON.toJSONString(idsMap);
                map.put("valuesSkuJson", strJson);
            },threadPoolExecutor);

            CompletableFuture<Void> attrCompletableFuture = CompletableFuture.runAsync(() -> {
                List<BaseAttrInfo> attrList = productFeignClient.getAttrList(skuId);
                List<HashMap<String, Object>> mapList = attrList.stream().map(baseAttrInfo -> {
                    HashMap<String, Object> mp = new HashMap<>();
                    mp.put("attrName", baseAttrInfo.getAttrName());
                    mp.put("attrValue", baseAttrInfo.getAttrValueList().get(0).getValueName());
                    return mp;
                }).collect(Collectors.toList());
                map.put("skuAttrList", mapList);
            },threadPoolExecutor);

            CompletableFuture<Void> spuCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
                List<SpuSaleAttr> spuSaleAttrListCheckBySku = productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
                map.put("spuSaleAttrList", spuSaleAttrListCheckBySku);
            },threadPoolExecutor);

            CompletableFuture<Void> spuPosterCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
                List<SpuPoster> posterList = productFeignClient.findSpuPosterBySpuId(skuInfo.getSpuId());
                map.put("spuPosterList", posterList);
            },threadPoolExecutor);

            CompletableFuture<Void> incrCompletableFuture = CompletableFuture.runAsync(() -> {
                listFeignClient.incrHotScore(skuId);
            },threadPoolExecutor);


            CompletableFuture.allOf(skuInfoCompletableFuture,
                                    priceCompletableFuture,
                                    categoryViewCompletableFuture,
                                    valueCompletableFuture,
                                    attrCompletableFuture,
                                    spuCompletableFuture,
                                    spuPosterCompletableFuture,
                                    incrCompletableFuture).join();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭线程池
            //threadPoolExecutor.shutdown();
        }

        return map;

    }
}
